package com.suli.multimedia.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.scenic.util.ResponseResult;
import com.suli.multimedia.entity.Hotel;
import com.suli.multimedia.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SuLi
 * @since 2022-04-18
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/hotel")
public class HotelController {

    @Autowired
    HotelService hotelService;

    /**
     * 创建hotel
     * @param hotel 旅馆
     * @return 创建结果
     */
    @PostMapping("/createHotel")
    public ResponseResult createHotel(@RequestBody Hotel hotel){
        Map<String, Object> hotelList = hotelService.getHotelList();
        Integer total = (Integer) hotelList.get("total");
        if(total>15)
            return ResponseResult.error().message("信息录入失败,最多只能录入15个旅馆信息");
        if(hotelService.save(hotel)){
            return ResponseResult.ok().message("信息录入成功");
        }
            return ResponseResult.error().message("信息录入失败");
    }

    /**
     * 获取旅馆列表
     * @return 旅馆列表
     */
    @GetMapping("/getHotelList")
    public ResponseResult getHotelList(){
        Map<String, Object> hotelList = hotelService.getHotelList();
        if(!ObjectUtils.isEmpty(hotelList)){
            return ResponseResult.ok().data(hotelList);
        }
            return ResponseResult.error().message("拉取旅馆列表失败");
    }

    /**
     * 删除旅馆信息
     * @param hotelId 旅馆id
     * @return 删除结果
     */
    @DeleteMapping ("/deleteHotel/{hotelId}")
    public ResponseResult deleteHotel(@PathVariable Integer hotelId){
        if(hotelService.deleteHotel(hotelId)){
            return ResponseResult.ok().message("删除成功");
        }
            return ResponseResult.error().message("删除失败");
    }

    /**
     * 根据旅馆id获取旅馆信息
     * @param hotelId 旅馆id
     * @return 旅馆信息
     */
    @GetMapping("/getHotelInfo/{hotelId}")
    public ResponseResult getHotelInfo(@PathVariable String hotelId){
        Hotel hotel = hotelService.getById(hotelId);
        if(!ObjectUtils.isEmpty(hotel))
        return ResponseResult.ok().data("hotelInfo",hotel);
        return ResponseResult.error().message("拉取信息失败");
    }

    /**
     * 更新旅馆信息
     * @param hotel 旅馆信息
     * @return 更新结果
     */
    @PutMapping("/updateHotel")
    public ResponseResult updateHotel(@RequestBody Hotel hotel){
        UpdateWrapper<Hotel> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("hotel_id", hotel.getHotelId());
        if(hotelService.update(hotel, updateWrapper)){
            return ResponseResult.ok().message("更新完成");
        }
        return ResponseResult.error().message("更新失败");
    }

    @DeleteMapping("/deleteHotelImage/{hotelId}")
    public ResponseResult deleteHotelImage(@PathVariable Integer hotelId){
        if(hotelService.deleteImage(hotelId)){
            return ResponseResult.ok().message("移除成功");
        }
        return ResponseResult.error().message("移除失败");
    }
}

